﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using CloudTaobao.Util;
using System.Windows.Controls.Primitives;

namespace CloudTaobao
{
    public partial class TradePage : PhoneApplicationPage
    {
        Find fd = new Find();
        AppSettings settings = new AppSettings();
        App app = Application.Current as App;
        public TradePage()
        {
            InitializeComponent();

            fd.transForm.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(transForm_PropertyChanged);
            fd.startLoadingEvent += new StartLoadingEventHandler(fd_startLoadingEvent);
            fd.endLoadingEvent += new EndLoadingEventHandler(fd_endLoadingEvent);
            fd.tradeList.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(tradeList_CollectionChanged);
            fd.transForm.transitStepInfos.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(transitStepInfos_CollectionChanged);

            DataContext = fd;
            TradeList.ItemsSource = fd.tradeList;
            TransStepList.ItemsSource = fd.transForm.transitStepInfos;
        }

        void transitStepInfos_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            if (TransStepList.Items.Count == 0 || TransStepList.Items.Count == 1)
                return;
            TransStepList.ScrollIntoView(TransStepList.Items[0]);
            TransStepList.UpdateLayout();
        }

        void tradeList_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            if (TradeList.Items.Count == 0 || TradeList.Items.Count == 1)
                return;
            TradeList.ScrollIntoView(TradeList.Items[0]);
            TradeList.UpdateLayout();
        }

        void fd_endLoadingEvent()
        {
            LoadingBar.IsIndeterminate = false;
            LoadingBar.Visibility = Visibility.Collapsed;
        }

        void fd_startLoadingEvent(object source, StartLoadingEventArgs args)
        {
            LoadingBar.IsIndeterminate = true;
            LoadingBar.Visibility = Visibility.Visible;
        }

        void transForm_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if ("out_sid".Equals(e.PropertyName))
                outId.Text = fd.transForm.out_sid;
            if ("company_name".Equals(e.PropertyName))
                companyName.Text = fd.transForm.company_name;
            if ("status".Equals(e.PropertyName))
                status.Text = fd.transForm.status;
        }

        private void refreshTrades_Click(object sender, EventArgs e)
        {
            try
            {
                fd.totalTrades = 0;
                fd.currentTradePage = 1;
                fd.GetTradeList(fd.currentTradePage, 10);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void lastTradesPage_Click(object sender, EventArgs e)
        {
            try
            {
                if (fd.currentTradePage > 1)
                {
                    fd.GetTradeList(--fd.currentTradePage, 10);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void nextTradesPage_Click(object sender, EventArgs e)
        {
            try
            {
                if ((fd.currentTradePage * 10 < fd.totalTrades))
                {
                    fd.GetTradeList(++fd.currentTradePage, 10);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void Pivot_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (pivotContrl.SelectedIndex == 0)
                ApplicationBar = (Microsoft.Phone.Shell.ApplicationBar)Resources["tradesBar"];
            else if (pivotContrl.SelectedIndex == 1)
                ApplicationBar = null;
        }

        private void TradeList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (TradeList != null && TradeList.SelectedIndex!=-1)
            {
                try
                {
                    TradeRecord tr = fd.tradeList.ElementAt<TradeRecord>(TradeList.SelectedIndex);
                    if (tr != null)
                        fd.GetTransFormList(tr.tid, tr.seller_nick);
                    if (tr.tid != "" && tr.seller_nick != "")
                    {
                        pivotContrl.SelectedIndex = 1;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                    
            }
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            fd.totalTrades = 0;
            fd.currentTradePage = 1;

            if (!app.sessionInfo.ContainsKey("top_session")) 
            {
                if (MessageBox.Show("抱歉^_^您需要登录才能使用此功能,是否转向登录页面？", "缺少登录信息",
                    MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    NavigationService.Navigate(new Uri("/LoginPage.xaml", UriKind.Relative));
                }
                else 
                {
                    NavigationService.GoBack();
                }
                return;
            }

            myTitle.Text = settings.nick;
            try
            {
                fd.GetTradeList(fd.currentTradePage, 10);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}